// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.dengues.core.i18n.Messages;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.widgets.Shell;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-5 qiang.zhang $
 * 
 */
public class ExceptionOperation {

    private static Logger logger = Logger.getLogger(ExceptionOperation.class);

    /**
     * Qiang.Zhang.Adolf@gmail.com ExceptionOperation constructor comment.
     */
    private ExceptionOperation() {
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "operate".
     * 
     * @param ex
     */
    public static void operate(Throwable ex) {
        Priority priority = getPriority(ex);
        String message = ex.getMessage();
        logger.log(priority, message, ex);
        if (priority == Level.FATAL) {
            openErrorDilogWithDetail(new Shell(), Messages.getString("ExceptionOperation.fatal"), ex.getMessage()); //$NON-NLS-1$
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "openErrorDilogWithDetail".
     * 
     * @param shell
     * @param mainMessage
     * @param detailMessage
     * @return
     */
    public static int openErrorDilogWithDetail(Shell shell, String mainMessage, String detailMessage) {
        MultiStatus info = new MultiStatus(DenguesCorePlugin.PLUGIN_ID, 1, mainMessage, null);
        info.add(new Status(IStatus.ERROR, DenguesCorePlugin.PLUGIN_ID, detailMessage));
        int openError = ErrorDialog.openError(new Shell(), Messages.getString("ExceptionOperation.error"), null, info); //$NON-NLS-1$
        return openError;
    }

    /**
     * Return priority corresponding to the exception implementation.
     * 
     * @param ex - the exception to evaluate priority
     * @return the priority corresponding to the exception implementation
     */
    protected static Priority getPriority(Throwable ex) {
        return Level.ERROR;
    }
}
